package com.ruoyi.WELD.controller;
import com.ruoyi.WELD.domain.DataStation;
import com.ruoyi.WELD.domain.Welding;
import com.ruoyi.WELD.service.IDataStationService;
import com.ruoyi.WELD.service.IWeldingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/DataStation/dataStation")
public class DataStationController extends BaseController
{
    @Autowired
    private IDataStationService dataStationService;

    @Autowired
    private IWeldingService weldingService;

    /*新增*/
    @PostMapping
    public AjaxResult add(@RequestBody Welding welding)
    {
        return toAjax(dataStationService.insertDataStation(welding));
    }

    /*转存*/
    @PostMapping("/saveSelected")
    public void saveSelected(@RequestBody Map<String, Long[]> request){
        Long[] Ids = request.get("ids");
        List<Welding> list=weldingService.selectWeldingByIds(Ids);
        for (Welding welding:list){
            dataStationService.insertDataStation(welding);
        }
    }
    @PostMapping("/saveAll")
    public void saveAll(Welding welding){
        List<Welding> list=weldingService.selectWeldingList(welding);
        for (Welding welding1:list){
            dataStationService.insertDataStation(welding1);
        }
    }

    /*查询*/
    @GetMapping("/list")
    public TableDataInfo list(DataStation dataStation) {
        startPage();
        List<DataStation> list = dataStationService.selectDataStationList(dataStation);
        return getDataTable(list);
    }

    /*获取*/
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(dataStationService.selectDataStationById(ID));
    }


    /*删除*/
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(dataStationService.deleteDataStationByIds(IDs));
    }

    /*清空数据*/
    @DeleteMapping("/clear")
    public AjaxResult clearAllDataStationing()
    {
        return toAjax(dataStationService.ClearDataStation());
    }

    /*导出*/
    @PostMapping("/export")
    public void export(Long[] ids,HttpServletResponse response)
    {
        List<DataStation> list = dataStationService.selectDataStationByIds(ids);
        ExcelUtil<DataStation> util = new ExcelUtil<DataStation>(DataStation.class);
        util.exportExcel(response, list, "数据");
    }

    /*导出全部，默认全选*/
    @PostMapping("/exportAll")
    public void exportAll(DataStation dataStation, HttpServletResponse response)
    {
        List<DataStation> list = dataStationService.selectDataStationList(dataStation);
        ExcelUtil<DataStation> util = new ExcelUtil<DataStation>(DataStation.class);
        util.exportExcel(response, list, "数据");
    }


}